<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Results</title>
    </head>
    <body>
        <h1>Results</h1>
        <?php
        // 创建变量
        $searchtype=$_POST['searchtype'];
        $searchterm=trim($_POST['searchterm']);

        if(!$searchtype || !$searchterm) {
            echo "您还没有输入内容，请返回重试。";
            exit;
        }

        if(!get_magic_quotes_gpc()) {
            $searchtype=addslashes($searchtype);
            $searchterm=addslashes($searchterm);
        }

        echo $searchtype."<br>".$searchterm;

        @$db=new mysqli('localhost','book','admin','books');

        if(mysqli_connect_errno()) {
            echo '无法连接数据库';
            exit;
        }
        $query = "select * from books where ".$searchtype." like '%".$searchterm."%'";
        // 多余的空格会导致无法查询
        $result = $db->query($query);
        $num_results=$result->num_rows;

        echo "<p>搜索到的书本数量".$num_results."</p>";





        for($i=0;$i<$num_results;$i++) {
            /*
            $row = $result->fetch_assoc();
            echo "<p><strong>".($i+1).". Title: ";
            echo htmlspecialchars(stripslashes($row['title']));
            echo "</strong><br />Author: ";
            echo stripslashes($row['author']);
            echo "<br />ISBN: ";
            echo stripslashes($row['isbn']);
            echo "<br />Price: ";
            echo stripslashes($row['price']);
            echo "</p>";
            */

            $row=mysqli_fetch_row($result);
            echo "<p><strong>".($i+1).". Title: ";
            echo htmlspecialchars(stripslashes($row[2]));
            echo "</strong><br />Author: ";
            echo stripslashes($row[1]);
            echo "<br />ISBN: ";
            echo stripslashes($row[0]);
            echo "<br />Price: ";
            echo stripslashes($row[3]);
            echo "</p>";

/*
            $row = $result->fetch_object();
            echo "<p><strong>".($i+1).". Title: ";
            echo htmlspecialchars(stripslashes($row->title));
            echo "</strong><br />Author: ";
            echo stripslashes($row->author);
            echo "<br />ISBN: ";
            echo stripslashes($row->isbn);
            echo "<br />Price: ";
            echo stripslashes($row->price);
            echo "</p>";
*/
        }

        $result->free();
        $db->close();









         ?>
    </body>
</html>
